self.addEventListener("fetch", function (event) {
})

self.addEventListener('notificationclick', (event) => {
  const notification = event.notification
  notification.close()
  const chat = notification.data
  const windowUrl = 'https://www.qingchi123.com/message/' + chat.id
  if (chat) {
    let found = false
    event.waitUntil(
      clients.matchAll().then((clientsArr) => {
        for (const item of clientsArr) {
          if (item.url === windowUrl) {
            // We already have a window to use, focus it.
            found = true
            item.focus()
            break
          }
        }
        if (!found) {
          // Create a new window.
          clients.openWindow(windowUrl)
        }
      })
    )
  }
})
